﻿// -----------------------------------------------------------------------
// <copyright file="YIBIYIMVCBuilder.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ConsoleApplication1.YIBIYIMVC
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.IO;
    using DotNet.Schema;
    using System.Data.OleDb;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class YIBIYIMVCBuilder
    {
        private string _namespace;
        private string _webProjNameSpace;
        private string _dbConnection;
        private string _targetFolder;
        private string _databaseName;
        private string _tableName;

        public YIBIYIMVCBuilder(string dbConnStr, string dbName, string targetFoler, string tableName, string ns, string webns)
        {
            if (string.IsNullOrEmpty(dbName))
                throw new Exception("数据库名不能为空。");

            if (string.IsNullOrEmpty(dbConnStr))
                throw new Exception("数据库链接不能为空。");

            if (string.IsNullOrEmpty(targetFoler))
                throw new Exception("目标文件夹不能为空。");

            this._namespace = ns;
            this._webProjNameSpace = webns;
            this._dbConnection = dbConnStr;
            this._targetFolder = Path.Combine(targetFoler, "");//AutoEntites
            this._databaseName = dbName;
            this._tableName = tableName;

            try
            {
                if (!Directory.Exists(this._targetFolder))
                {
                    Directory.CreateDirectory(this._targetFolder);
                }
                else
                {
                    Directory.Delete(this._targetFolder, true);
                    Directory.CreateDirectory(this._targetFolder);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("目标文件夹创建失败。");
            }
        }

        public string Build()
        {
            ServerInfo serverInfo = new ServerInfo(this._dbConnection, "ceshi");

            List<string> filePaths = new List<string>();

            DatabaseInfo database = serverInfo.GetDatabase(_databaseName); ;

            foreach (TableInfo tInfo in database.Tables)
            {
                if(tInfo.LowerName != _tableName.ToLower())
                {
                    continue;
                }
                tInfo.Name = _tableName;

                MapBuild(tInfo);

                ModelBuild(tInfo);

                DTOBuild(tInfo);

                RepositoryBuild(tInfo);

                IServiceBuild(tInfo);

                ServiceBuild(tInfo);

                //ViewModelBuild(tInfo);

                //ControllerBuild(tInfo);

                //IndexViewBuild(tInfo);

                //CreateViewBuild(tInfo);

                //EditViewBuild(tInfo);

            }
            return this._targetFolder;
        }

        private void EditViewBuild(TableInfo tInfo)
        {
            EditView tableCode = new EditView(tInfo, this._webProjNameSpace);
            string codeString = tableCode.TransformText();
            string filePath = Path.Combine(_targetFolder, "Edit.cshtml");
            if (!Directory.Exists(_targetFolder))
            {
                Directory.CreateDirectory(_targetFolder);
            }
            using (var sw = new StreamWriter(File.Open(filePath, FileMode.CreateNew), Encoding.UTF8))
            {
                sw.Write(codeString);
            }
        }

        private void CreateViewBuild(TableInfo tInfo)
        {
            CreateView tableCode = new CreateView(tInfo, this._webProjNameSpace);
            string codeString = tableCode.TransformText();
            string filePath = Path.Combine(_targetFolder, "Add.cshtml");
            if (!Directory.Exists(_targetFolder))
            {
                Directory.CreateDirectory(_targetFolder);
            }
            using (var sw = new StreamWriter(File.Open(filePath, FileMode.CreateNew), Encoding.UTF8))
            {
                sw.Write(codeString);
            }
        }

        private void IndexViewBuild(TableInfo tInfo)
        {
            IndexView tableCode = new IndexView(tInfo, this._webProjNameSpace);
            string codeString = tableCode.TransformText();
            string filePath = Path.Combine(_targetFolder, "Index.cshtml");
            if (!Directory.Exists(_targetFolder))
            {
                Directory.CreateDirectory(_targetFolder);
            }
            using (var sw = new StreamWriter(File.Open(filePath, FileMode.CreateNew), Encoding.UTF8))
            {
                sw.Write(codeString);
            }
        }

        private void ControllerBuild(TableInfo tInfo)
        {
            Controller tableCode = new Controller(tInfo, this._namespace, this._webProjNameSpace);
            string codeString = tableCode.TransformText();
            //string filePath = Path.Combine(_targetFolder, tInfo.PascalName + "Controller.cs");
            string filePath = Path.Combine(_targetFolder, tInfo.Name + "Controller.cs");
            if (!Directory.Exists(_targetFolder))
            {
                Directory.CreateDirectory(_targetFolder);
            }
            using (var sw = new StreamWriter(File.Open(filePath, FileMode.CreateNew), Encoding.UTF8))
            {
                sw.Write(codeString);
            }
        }

        private void ViewModelBuild(TableInfo tInfo)
        {
            ViewModel tableCode = new ViewModel(tInfo, this._namespace, this._webProjNameSpace);
            string codeString = tableCode.TransformText();
            //string filePath = Path.Combine(_targetFolder, tInfo.PascalName + "ViewModel.cs");
            string filePath = Path.Combine(_targetFolder, tInfo.Name + "ViewModel.cs");
            if (!Directory.Exists(_targetFolder))
            {
                Directory.CreateDirectory(_targetFolder);
            }
            using (var sw = new StreamWriter(File.Open(filePath, FileMode.CreateNew), Encoding.UTF8))
            {
                sw.Write(codeString);
            }
        }

        private void RepositoryBuild(TableInfo tInfo)
        {
            Repository tableCode = new Repository(tInfo, this._namespace);
            string codeString = tableCode.TransformText();
            //string filePath = Path.Combine(_targetFolder, tInfo.PascalName + "Repository.cs");
            string filePath = Path.Combine(_targetFolder, tInfo.Name + "Repository.cs");
            if (!Directory.Exists(_targetFolder))
            {
                Directory.CreateDirectory(_targetFolder);
            }
            using (var sw = new StreamWriter(File.Open(filePath, FileMode.CreateNew), Encoding.UTF8))
            {
                sw.Write(codeString);
            }
        }

        private void ServiceBuild(TableInfo tInfo)
        {
            Service tableCode = new Service(tInfo, this._namespace);
            string codeString = tableCode.TransformText();
            //string filePath = Path.Combine(_targetFolder, tInfo.PascalName + "Service.cs");
            string filePath = Path.Combine(_targetFolder, tInfo.Name + "ServiceImpl.cs");
            if (!Directory.Exists(_targetFolder))
            {
                Directory.CreateDirectory(_targetFolder);
            }
            using (var sw = new StreamWriter(File.Open(filePath, FileMode.CreateNew), Encoding.UTF8))
            {
                sw.Write(codeString);
            }
        }

        private void IServiceBuild(TableInfo tInfo)
        {
            IService tableCode = new IService(tInfo, this._namespace);
            string codeString = tableCode.TransformText();
            //string filePath = Path.Combine(_targetFolder, "I" + tInfo.PascalName + "Service.cs");
            string filePath = Path.Combine(_targetFolder, "I" + tInfo.Name + "Service.cs");
            if (!Directory.Exists(_targetFolder))
            {
                Directory.CreateDirectory(_targetFolder);
            }
            using (var sw = new StreamWriter(File.Open(filePath, FileMode.CreateNew), Encoding.UTF8))
            {
                sw.Write(codeString);
            }
        }

        private void ModelBuild(TableInfo tInfo)
        {
            Model tableCode = new Model(tInfo, this._namespace);
            string codeString = tableCode.TransformText();
            //string filePath = Path.Combine(_targetFolder, tInfo.PascalName + ".cs");
            string filePath = Path.Combine(_targetFolder, tInfo.Name + ".cs");
            if (!Directory.Exists(_targetFolder))
            {
                Directory.CreateDirectory(_targetFolder);
            }
            using (var sw = new StreamWriter(File.Open(filePath, FileMode.CreateNew), Encoding.UTF8))
            {
                sw.Write(codeString);
            }
        }

        private void DTOBuild(TableInfo tInfo)
        {
            DTO tableCode = new DTO(tInfo, this._namespace);
            string codeString = tableCode.TransformText();
            //string filePath = Path.Combine(_targetFolder, tInfo.PascalName + "DTO.cs");
            string filePath = Path.Combine(_targetFolder, tInfo.Name + "DTO.cs");
            if (!Directory.Exists(_targetFolder))
            {
                Directory.CreateDirectory(_targetFolder);
            }
            using (var sw = new StreamWriter(File.Open(filePath, FileMode.CreateNew), Encoding.UTF8))
            {
                sw.Write(codeString);
            }
        }

        private void MapBuild(TableInfo tInfo)
        {
            Map tableCode = new Map(tInfo, this._namespace);
            string codeString = tableCode.TransformText();
            //string filePath = Path.Combine(_targetFolder, tInfo.PascalName + "Map.cs");
            string filePath = Path.Combine(_targetFolder, tInfo.Name + "Map.cs");
            if (!Directory.Exists(_targetFolder))
            {
                Directory.CreateDirectory(_targetFolder);
            }
            using (var sw = new StreamWriter(File.Open(filePath, FileMode.CreateNew), Encoding.UTF8))
            {
                sw.Write(codeString);
            }
        }
    }
}
